import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class BasicColorDialogDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        """初始化用户界面"""
        layout = QVBoxLayout()
        
        # 颜色显示区域
        self.color_display = QLabel('当前颜色')
        self.color_display.setAlignment(Qt.AlignCenter)
        self.color_display.setMinimumHeight(100)
        self.color_display.setStyleSheet("background-color: white; border: 2px solid black;")
        
        # 信息标签
        self.info_label = QLabel('请点击按钮选择颜色')
        self.info_label.setAlignment(Qt.AlignCenter)
        
        # 选择颜色按钮
        select_btn = QPushButton('选择颜色')
        select_btn.clicked.connect(self.select_color)
        
        layout.addWidget(self.color_display)
        layout.addWidget(self.info_label)
        layout.addWidget(select_btn)
        
        self.setLayout(layout)
        self.setWindowTitle('基本颜色对话框演示')
        
    def select_color(self):
        """打开颜色选择对话框"""
        from PyQt5.QtWidgets import QColorDialog
        
        # 基本用法 - 使用默认设置
        color = QColorDialog.getColor()
        
        # 检查颜色是否有效（用户是否点击了OK）
        if color.isValid():
            # 更新颜色显示
            self.color_display.setStyleSheet(f"background-color: {color.name()}; border: 2px solid black;")
            
            # 显示颜色信息
            self.info_label.setText(
                f"RGB: ({color.red()}, {color.green()}, {color.blue()})\n"
                f"HEX: {color.name()}"
            )
        else:
            self.info_label.setText("用户取消了颜色选择")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = BasicColorDialogDemo()
    window.show()
    sys.exit(app.exec_())
